home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 17 controls / customcontroldemo / userpropextender.vb < prev   
Encoding:
Text File  |  2002-03-16  |  3.4 KB  |  102 lines

  1. Imports System.ComponentModel
  2.  
  3. ' Let the form know that this control will add the UserRole property.
  4.  
  5. <ProvideProperty("UserRole", GetType(Control))> _
  6. Public Class UserPropExtender
  7.     Inherits System.ComponentModel.Component
  8.  
  9.     Implements IExtenderProvider
  10.  
  11.     ' This function must return True for all controls that can be extended with
  12.     ' the UserRole property
  13.     Public Function CanExtend(ByVal extendee As Object) As Boolean Implements System.ComponentModel.IExtenderProvider.CanExtend
  14.         ' extend all controls except this one
  15.         ' (not really necessary in this case, because this is a Component, not a control.)
  16.         If Not (TypeOf extendee Is UserPropExtender) Then
  17.             Return True
  18.         End If
  19.     End Function
  20.  
  21.     ' This is the Hashtable that holds the association between controls 
  22.     ' and their UserRole.
  23.     Dim userRoleValues As New Hashtable()
  24.  
  25.     ' These are the Get/Set methods related to the property being added.
  26.  
  27.     Function GetUserRole(ByVal ctrl As Control) As String
  28.         ' Check if there is a property associated to this control.
  29.         Dim value As Object = userRoleValues(ctrl)
  30.         ' Return the value found, or an empty string
  31.         If value Is Nothing Then
  32.             Return ""
  33.         Else
  34.             Return value.ToString
  35.         End If
  36.     End Function
  37.  
  38.     Sub SetUserRole(ByVal ctrl As Control, ByVal value As String)
  39.         ' in case it is passed Nothing.
  40.         If Value Is Nothing Then Value = ""
  41.  
  42.         If Value.Length = 0 And userRoleValues.Contains(ctrl) Then
  43.             ' remove the control from the hashtable
  44.             userRoleValues.Remove(ctrl)
  45.             ' remove event handlers, if any
  46.             ' ...
  47.         ElseIf Value.Length > 0 Then
  48.             If Not userRoleValues.Contains(ctrl) Then
  49.                 ' add any event handlers here
  50.                 ' ...
  51.             End If
  52.             ' assign the new value.
  53.             userRoleValues.Item(ctrl) = Value
  54.             ' refresh this control.
  55.             SetControlVisibility(ctrl)
  56.         End If
  57.     End Sub
  58.  
  59.     ' this property can be assigned the name of the current user.
  60.  
  61.     Dim m_CurrentUserRole As String
  62.  
  63.     Property CurrentUserRole() As String
  64.         Get
  65.             Return m_CurrentUserRole
  66.         End Get
  67.         Set(ByVal Value As String)
  68.             m_CurrentUserRole = Value
  69.             RefreshAllControls()
  70.         End Set
  71.     End Property
  72.  
  73.     ' hide/show all controls based on their UserRole property
  74.  
  75.     Sub RefreshAllControls()
  76.         Dim ctrl As Control
  77.         For Each ctrl In userRoleValues.Keys
  78.             SetControlVisibility(ctrl)
  79.         Next
  80.     End Sub
  81.  
  82.     ' hide/show a single control based on its UserRole property
  83.  
  84.     Private Sub SetControlVisibility(ByVal ctrl As Control)
  85.         ' do nothing if no user has been defined
  86.         If CurrentUserRole = "" Then Exit Sub
  87.         ' do nothing if the control isn't in the hash table
  88.         If Not userRoleValues.Contains(ctrl) Then Exit Sub
  89.  
  90.         ' get the value in the hash table
  91.         Dim value As String = userRoleValues(ctrl).ToString
  92.  
  93.         ' check whether the current user is one of the defined users
  94.         If InStr(";" & value & ";", ";" & CurrentUserRole & ";", CompareMethod.Text) > 0 Then
  95.             ctrl.Visible = True
  96.         Else
  97.             ctrl.Visible = False
  98.         End If
  99.     End Sub
  100.  
  101. End Class
  102.